import {
  getTokenMsg
} from '../../config/apiServe/fetch-config'
import {
  getTradeSymbol
} from '../../utils/filters'
export default {
  data() {
    return {
      para: require('../../assets/img/computer/paracoin.png'),
      base: require('../../assets/img/coin/baseCoin.png'),
      Loading: false,
      block: {
        txCount: 0,
        height: 0,
        hash: "",
        blockTime: "",
        txHash: "",
        stateHash: ""
      },
      txHashes: [],
      Trades: [],
      pages: {
        currentPage: 1,
        pageSize: 10,
        total: 100
      },
    };
  },
  computed: {
    maxHeight() {
      return this.$store.state.maxHeight
    }
  },
  watch: {
    $route() {
      this.pages = {
        currentPage: 1,
        pageSize: 10
      }
      this.init()
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // init
    init() {
      if (this.$route.query.height >= 0) {
        this.block.height = this.$route.query.height
        this.getBlockHash();
      } else {
        this.$notify({
          title: this.$t('components.tip'),
          message: this.$t('components.errParam'),
          type: "warning",
          duration: 1000
        });
        this.$router.push({
          path: "/"
        });
      }
    },
    // 获取区块哈希
    getBlockHash() {
      this.Rpc.getBlockHash(Number(this.block.height)).then(data => {
        if (data.error === null) {
          this.block.hash = data.result.hash
          this.getBlockInfo(this.block.hash)
        } else {
          this.$notify({
            title: this.$t('components.tip'),
            message: data.error,
            type: "warning"
          });
        }
      })
    },
    // 获取区块概况
    getBlockInfo(hash) {
      this.Rpc.getBlockOverview(hash).then(data => {
        if (data.error === null) {
          this.block = data.result.head
          this.pages.total = data.result.txCount
          this.txHashes = data.result.txHashes
          if (this.txHashes === null || this.txHashes.length === 0) {
            this.Loading = false
            return
          }
          this.getTrades()
        } else {
          this.$notify({
            title: this.$t('components.tip'),
            message: data.error,
            type: "warning"
          });
        }
      })
    },
    // 获取交易详情
    getTrades() {
      this.Loading = true
      let searchArr = []
      let end = this.pages.pageSize * this.pages.currentPage - 1 < this.pages.total ? this.pages.pageSize * this.pages.currentPage - 1 : this.pages.total - 1
      let start = this.pages.pageSize * (this.pages.currentPage - 1)
      for (let i = start; i <= end; i++) {
        searchArr.push(this.txHashes[i])
      }
      this.Rpc.getTxByHashes(searchArr).then(data => {
        if (data.error == null) {
          data.result.txs.forEach(item => {
            item.symbol = getTradeSymbol(item)
          })
          this.searchIcon(data.result.txs)
        } else {
          this.Loading = false
          this.$notify({
            title: this.$t('components.tip'),
            type: "warning",
            message: data.error
          });
        }
      })
    },
    searchIcon(trades) {
      let base = this.$store.state.baseCoinIcon
      let promises = trades.map(item => {
        if (item.symbol === this.$store.state.baseCoin) return base
        else if (item.symbol === 'para') return this.para
        else {
          return getTokenMsg(item.symbol).then(res => {
            if (res.code === 0) return res.data[0].icon === '' ? this.base : res.data[0].icon
          }).catch(() => {
            return this.base
          })
        }
      })
      Promise.all(promises).then(res => {
        trades.map((item, index) => {
          item.icon = res[index]
        })
        this.Trades = this.checkGroup(trades)
        console.log(this.Trades)
        this.Loading = false
      })
    },
    pageChange(page) {
      this.pages.currentPage = page;
      this.getTrades()
    },
    sizeChange(size) {
      this.pages.pageSize = size;
    },
    // 交易组模块
    groupCellName(val) {
      switch (val.row.tradeG) {
        case 0:
          break;
        case 1:
          return 'tx-middle'
        case 2:
          return 'tx-start'
        case 3:
          return 'tx-end'
        default:
          break;
      }
    },
    //区分交易组
    checkGroup(arr) {
      arr.map((current, index) => {
        if (current.tx.groupCount > 1) {
          current.tradeG = 1 //0-非交易组，1-交易组，3-交易组末条, 2-交易组首条
          // current.tradeG = (current.tx.fee === 0 && arr[index-1].tx.fee!==0)? 2 : 1
          if (current.tx.fee !== 0) {
            current.tradeG = 2
          } else {
            if (index < arr.length) {
              current.tradeG = arr[index].tx.next ? 1 : 3
            } else {
              current.tradeG = 3
            }
          }
        } else {
          current.tradeG = 0
        }
      })
      return arr
    },
    success() {
      this.$message.success(this.$t('components.copySuc'));
    },
    error() {
      this.$message.warning(this.$t('components.copyErr'));
    }
  }
};